﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using TAMS;

namespace TAMS
{
    public partial class Admin_TAModify : System.Web.UI.Page
    {
        MySqlConnection conn;
        MySqlCommand sqlCommand;
        MySqlDataReader readVals;
        string enteredID;
        static string enteredName;

        string strProvider = Resource1.databaseConnection;

        protected void Page_Load(object sender, EventArgs e)
        {
            ((Menu)Master.FindControl("Menu1")).Items[3].Selected = true;
            Master.FindControl("ContentPlaceholder1").FindControl("Button2").Visible = false;
            Master.FindControl("ContentPlaceholder1").FindControl("Button3").Visible = false;
            Master.FindControl("ContentPlaceholder1").FindControl("Button4").Visible = false;

            enteredID = Request.QueryString["id"];
            

            if (!IsPostBack)
            {
                // Connect to the Database
                conn = new MySqlConnection(strProvider);
                conn.Open();

                // Get the type
                

                string comma = ", ";

                string getSelectedInfo = "SELECT *"
                                     + "FROM GradStudent S, TA F "
                                     + "WHERE S.sid = " + enteredID +" AND F.ta_sid = S.sid";

                sqlCommand = new MySqlCommand(getSelectedInfo, conn);
                readVals = sqlCommand.ExecuteReader();

                readVals.Read();
                ID.Text = ""+readVals.GetInt32("sid");
                Name.Text = "" + readVals.GetString("s_name");
                enteredName = Name.Text.ToString();
                BDate.Text = "" + readVals.GetDateTime("s_birthDate").ToString("dd-MM-yyyy");
                EDate.Text = "" + readVals.GetDateTime("s_entryDate").ToString("dd-MM-yyyy");
                Email.Text = "" + readVals.GetString("s_email");
                Password.Text = "" + readVals.GetString("s_pass");
                ImgUrl.Text = readVals.GetString("s_url");

                readVals.Close();

            }

  

        }

        protected void Add_Click(object sender, EventArgs e)
        {
            /*Get info from textboxes*/

            string id = ID.Text;
            string name = Name.Text;

            DateTime bdate = Convert.ToDateTime(BDate.Text);
            string bdateInfo = bdate.Year + "-" + bdate.Month + "-" + bdate.Day;

            DateTime edate = Convert.ToDateTime(EDate.Text);
            string edateInfo = edate.Year + "-" + edate.Month + "-" + edate.Day;

            string email = Email.Text;
            string pass = Password.Text;
            string url = ImgUrl.Text;


            // Connect to the Database
            conn = new MySqlConnection(strProvider);
            conn.Open();

            string comma = ", ";

            string modifyStaff = "UPDATE GradStudent "
                                 + " SET  sid=" + id + comma +" s_name=" + "'" +name + "'"+ comma + "s_birthDate=" + "'" +bdateInfo + "'" + comma + "s_entryDate="+"'" +edateInfo + "'"+comma+"s_email="+"'" +email + "'"+comma+"s_pass="+"'" +pass + "'"+comma+"s_url="+"'" +url + "'"
                                 + " WHERE sid="+enteredID;

            string modifyfMember = "UPDATE TA "
                                 + "SET  ta_sid="+"'"+id+"'"
                                 + " WHERE ta_sid="+enteredID;
              

            sqlCommand = new MySqlCommand(modifyStaff, conn);

            Boolean checker = false;
            try
            {
                sqlCommand.ExecuteNonQuery();

                sqlCommand = new MySqlCommand(modifyfMember, conn);

                sqlCommand.ExecuteNonQuery();
            }
            catch (MySqlException ex)
            {

                string msg = "Insert Error:";
                msg += ex.Message;
                ErrorLiteral.Text = msg;
                ErrorLiteral.Visible = true;
                checker = true;

            }

            if (!checker)
            {
                Response.Redirect("Admin_TA.aspx");
            }
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Admin_TA.aspx");
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            string url = "../editHousing.aspx?name=" + enteredName + "&type=student&id=" + enteredID;

            ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>openNewWin('" + url + "')</script>");

        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            string url = "../editOffice.aspx?name=" + enteredName + "&type=student&id=" + enteredID;

            ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>openNewWin('" + url + "')</script>");
        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            string url = "../editCourses.aspx?name=" + enteredName + "&type=student&id=" + enteredID;

            ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>openNewWin('" + url + "')</script>");
        }
    }
    
}